<?php

namespace app\api\controller;
use data\Json;
use think\Request;
use JPush\Client as JPush;
use app\common\model\TravelMember;
use app\common\model\Travel as TravelModel;
use app\common\model\Activity;
/**
 * 发现
 */
class Blog extends Base
{
    protected $user_model = null;
    protected $token = null;

    public function _initialize()
    {
        $this->user_model = model('User');
        $this->token = request()->header('token');
        //$this->user = $this->user_model->getUserInfo($this->token);
        $this->user = $this->user_model->get('1');
    }

    // 添加发现时返回的活动列表
    public function travels()
    {
        if(!$this->user)return Json::json_send(1);
        $map['sponsor'] = $this->user['id'];
        $travelList = TravelModel::where($map)->order('createtime desc')->select();
        if($this->user['role'] == 2)
        {
            $travel_ids = model('TravelMember')->where(['member_id'=>$this->user['id']])->column('travel_id');
            $map2['id']  = ['in',implode(',', $travel_ids)];
            $travelList = TravelModel::where($map)->whereOr($map2)->order('createtime desc')->select();
        }
        $data = [];
        foreach ($travelList as $k => $v) 
        {
            $categorys = [];
            $titles = [];
            $fee = 0;
           // $alist = [];
            if($v['type'] == 0)
            {
                $activtities = model('TravelActivity')->alias('ta')
                                ->join('Activity a','ta.activity_id=a.id')
                                ->where(['ta.travel_id'=>$v['id']])
                                ->select();
                foreach ($activtities as $ak => $av) {
                    array_push($titles, $av['name']);
                }
            }
            else if($v['type'] == 1)
            {
                $activtities = model('TravelActivity')->alias('ta')
                                ->join('oversea a','ta.activity_id=a.id')
                                ->where(['ta.travel_id'=>$v['id']])
                                ->select();
                foreach ($activtities as $ak => $av) {
                    array_push($titles, $av['name']);
                }
            }

            $data[$k] = [
                'id'         => $v['id'],
                'titles'     => implode('+',$titles),
            ];
        }
        return Json::json_send(0,'',$data);
    }

    // 发现
    public function index(Request $request)
    {
        if(!$this->user)return Json::json_send(1);
        if($request->isPost())
        {
            $data = input();
            $travel=model('travel')->get($data['travel_id']);
            $a_model = $travel['type']==1?model('Oversea'):model('Activity');
            $travel_activities = $a_model->alias('a')
                                    ->join('travel_activity ta','a.id=ta.activity_id')
                                    ->field('a.*,ta.begintime start,ta.endtime end')
                                    ->where(['ta.travel_id'=>$data['travel_id']])->select();
            $categorys = [];
            $titles = [];
            foreach ($travel_activities as $tk => $tv) {
                array_push($categorys, Activity::getCategory($tv['category']));
                array_push($titles, $tv['name']);
            }
            $data['travel'] = empty($categorys)? '海外游学':implode('+',$categorys);
            $data['name'] = implode('+',$titles);
            $data['user_id']=$this->user['id'];
            if(model('blog')->allowField(true)->save($data))
            {
                $images = $this->multiUpload(request()->file('images'));
                $list = [];
                foreach ($images as $k => $v) {
                    $list[] = ['blog_id'=>model('blog')->id,'img'=>$v];
                }
                model('BlogPhotos')->saveAll($list);
                return Json::json_send(0);
            }
            return Json::json_send(9);
        }
        $blogs = model('blog')->order('createtime desc')->select();
        $advertisements = model('advertisement')->where(['status'=>'normal'])->order('createtime desc')->select();
        $return['advertisement'] = [];
        foreach ($advertisements as $k => $v) {
            $return['advertisement'][]=[
                'id'    =>$v['id'],
                'name'  =>$v['name'],
                'avatar'=>$v['avatar'],
                'image' =>$v['image'],
                'desc'  =>$v['desc'],
                'begintime' =>$v['begintime'],
                'endtime'   =>$v['endtime'],
            ];
        }
        $return['blog'] = [];
        foreach ($blogs as $k => $v) {
            $zans = model('BlogZan')->all(['blog_id'=>$v['id']]);
            $comments = model('BlogComment')->with('user')->where(['blog_id'=>$v['id']])->select();
            $photos = model('BlogPhotos')->where(['blog_id'=>$v['id']])->column('img');
            $sponsor = model('User')->get($v['user_id']);
            $return['blog'][]=[
                'id'      =>$v['id'],
                'userid'  =>empty($sponsor)?0:$sponsor['id'],
                'username'=>empty($sponsor)?'':$sponsor['name'],
                'avatar'  =>empty($sponsor)?'':$sponsor['avatar'],
                'createtime'=>$v['createtime'],
                'categorys' =>$v['categorys'],
                'name'      =>$v['name'],
                'content'    =>$v['content'],
                'createtime' =>$v['createtime'],
                'zclick'     =>count($zans),
                'commentNum' =>count($comments),
                'photos'     =>$photos,
                'is_zan'     =>empty(model('BlogZan')->where(['blog_id'=>$v['id'],'user_id'=>$this->user['id']])->find())?0:1,
            ];
        }
        return Json::json_send(0,'',$return);
    }

    public function myshares()
    {
        if(!$this->user)return Json::json_send(1);
        $map['user_id'] = $this->user['id'];
        $blogs = model('blog')::where($map)->order('createtime desc')->select();
        $return= [];
        foreach ($blogs as $k => $v) {
            $zans = model('BlogZan')->all(['blog_id'=>$v['id']]);
            $comments = model('BlogComment')->all(['blog_id'=>$v['id']]);
            $photos = model('BlogPhotos')->where(['blog_id'=>$v['id']])->column('img');
            $sponsor = model('User')->get($v['user_id']);
            $return[]=[
                'id'   =>$v['id'],
                'userid'=>$sponsor['id'],
                'username'=>$sponsor['name'],
                'avatar'=>$sponsor['avatar'],
                'createtime'=>$v['createtime'],
                'categorys' =>$v['categorys'],
                'name'      =>$v['name'],
                'content'    =>$v['content'],
                'createtime' =>$v['createtime'],
                'zclick'     =>count($zans),
                'commentNum' =>count($comments),
                'photos'     =>$photos,
                'is_zan'     =>empty(model('BlogZan')->where(['blog_id'=>$v['id'],'user_id'=>$this->user['id']])->find())?0:1,
            ];
        }
        return Json::json_send(0,'',$return);
    }

    public function zan(){
        if(!$this->user)return Json::json_send(1);
        $blog_id = input('blog_id',0);
        $zan = model('BlogZan')->get(['blog_id'=>$blog_id,'user_id'=>$this->user['id']]);
        if(!empty($zan))
        {
            if(model('BlogZan')->destroy($zan['id']))
            {
                return Json::json_send(0,'取消成功');
            }
            return Json::json_send(9);
        }

        $data = [
            'blog_id' => $blog_id,
            'user_id' => $this->user['id'],
        ];
        $result = model('BlogZan')->save($data);
        if($result !== false)
        {
            return Json::json_send(0,'点赞成功');
        }
        return Json::json_send(9);
    }

    public function comment(Request $request){
        if(!$this->user)return Json::json_send(1);
        $blog_id = input('blog_id',0);
        if($request->isPost())
        {
            $data = input();
            $data['user_id'] = $this->user['id'];
            $result = model('BlogComment')->allowField(true)->save($data);
            if($result !== false)
            {
                return Json::json_send(0);
            }
            return Json::json_send(9);
        }

        if(input('id'))
        {
            $result = model('BlogComment')->destroy(input('id'));
            if($result !== false)
            {
                return Json::json_send(0);
            }
            return Json::json_send(9);
        }

        $comments = model('BlogComment')->with('user')->where(['blog_id'=>$blog_id])->order('createtime')->select();
        $return = [];
        foreach ($comments as $k => $v) {
            $return[]=[
                'id'         => $v['id'],
                'user_id'    => $v['user']['id'],
                'user_name'  => $v['user']['name'],
                'user_avatar'=> $v['user']['avatar'],
                'content'    => $v['content'],
                'createtime' => $v['createtime']
            ];
        }

        return Json::json_send(0,'',$return);
    }

    public function delete()
    {
        if(!$this->user)return Json::json_send(1);
        $blog_id = input('blog_id',0);
        $result = model('Blog')->destroy($blog_id);
        if($result !== false)
        {
            model('BlogZan')->destroy(['blog_id'=>$blog_id]);
            model('BlogComment')->destroy(['blog_id'=>$blog_id]);
            model('BlogPhotos')->destroy(['blog_id'=>$blog_id]);
            return Json::json_send(0);
        }
        return Json::json_send(9);
    }

    public function advertisement()
    {
        $id = input('id',0);
        $show = input('show',0);
        if($show)
        {
            $this->view->assign('data',model('Advertisement')->get($id));
            return $this->view->fetch('index/advertisement');// 活动商
        }

        $url = url('blog/advertisement',['id'=>$id,'show'=>1]);
        return Json::json_send(0,'',$url);
        
    }
}
